package com.lagou.zuoye.servlet;

import com.lagou.zuoye.bean.User;
import com.lagou.zuoye.service.UserService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求中的用户名和密码信息并打印出来
        String userName = request.getParameter("userName");
        System.out.println("获取到的用户名信息是："+userName);
        String password = request.getParameter("password");
        System.out.println("获取到的密码信息是："+password);

        //2.创建UserService类型的对象去实现数据的校验功能
        UserService userService = new UserService();

        User user = userService.userLoginService(new User(userName, password));
        if(null==user){
            System.out.println("登录失败，用户名或者密码错误！");
            request.setAttribute("error","登录失败，用户名或者密码错误！");
            //实现服务器跳转，共享request和response    对象     使用转发的操作
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("login.jsp");
            requestDispatcher.forward(request,response);

        }else{
            System.out.println("登录成功，欢迎使用！");
            //将登录成功的用户信息放入session对象中实现多个请求共享
            request.getSession().setAttribute("user",user);
            response.sendRedirect("list.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
